package cuiyt.datastructure.list;

/**
 * @author cyt
 * @create 2020-11-18 19:52
 */
public class CyList<E> {
    private Integer size = 0;
    private E[] elements;
    private static final int DEFAULT_CAPACITY = 3;
    private static final int ELEMENT_NOT_FOUND = -1;

    public CyList() {
        this(DEFAULT_CAPACITY);
    }

    public CyList(int initialCapacity) {
        initialCapacity = initialCapacity < 0 ? DEFAULT_CAPACITY : initialCapacity;
        elements = (E[]) new Object[initialCapacity];
    }

    private void OutOfBounds(int index) {
        throw new IndexOutOfBoundsException("Index: " + index + "Size : " + size);
    }

    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            OutOfBounds(index);
        }
    }

    private void rangeCheckOfAdd(int index) {
        if (index < 0 || index > size) {
            OutOfBounds(index);
        }
    }

    public E remove(int index) {
        rangeCheck(index);
        E old = elements[index];
        for (int i = index + 1; i <= size - 1; i++) {
            elements[i - 1] = elements[i];
        }
        size--;
        elements[size] = null;
        return old;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void clear() {
        for (int i = 0; i < size; i++) {
            elements[i] = null;
        }
        size = 0;
    }

    public E get(int index) {
        rangeCheck(index);
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + "Size : " + size);
        }
        return elements[index];
    }

    public void add(E e) {
        add(size, e);
    }

    public int indexOf(E element) {
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (element.equals(elements[i])) {
                    return i;
                }
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (elements[i].equals(element)) {
                    return i;
                }
            }
        }
        return -1;
    }

    public E add(int index, E element) {
        rangeCheckOfAdd(index);
        ensureCapacity(size + 1);
        E old = elements[index];
        for (int i = size - 1; i >= index; i--) {
            elements[i + 1] = elements[i];
        }
        elements[index] = element;
        size++;
        return old;
    }

    private void ensureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if (oldCapacity >= capacity) {
            return;
        }
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
        System.out.println(oldCapacity + " =  > > " + newCapacity);
    }

    public void set(int index, E element) {
        rangeCheck(index);
        ensureCapacity(size + 1);
        elements[index] = element;
    }

    public int size() {
        return size;
    }

    public boolean contain(E element) {
        return indexOf(element) == ELEMENT_NOT_FOUND;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("size = ").append(size).append("  [");
        for (Integer i = 0; i < size; i++) {
            builder.append(elements[i]);
            if (i != size - 1) {
                builder.append(",");
            }
        }
        builder.append("]");
        return builder.toString();
    }
}
